﻿using System;
using System.Collections.Generic;
using System.Text;
using CookComputing.XmlRpc;
using System.Management;
using System.Net.Sockets;
namespace CUCTV
{
    [XmlRpcUrl("http://202.205.16.49:443")]
    public interface ICuctv : IXmlRpcProxy
    {
        [XmlRpcMethod("getVersion")]
        string GetVersion();
        [XmlRpcMethod("setMyInfo")]
        int SetMyINfo(string mac,string ip,string os,string username,string mp);
    }
    public class Utility
    {
       
        /// <summary>
        /// 从服务器取得最新版本信息
        /// </summary>
        /// <returns></returns>
        public static string GetNewestVersion()
        {
            try
            {
                ICuctv server = XmlRpcProxyGen.Create<ICuctv>();
               
                return server.GetVersion();
            }
            catch {
                return "";
            }
        }
      
        public static string CurrentVersion()
        {
            string version = System.Windows.Forms.Application.ProductVersion;
            string[] v = version.Split(new Char[] { '.' });

            return String.Format("{0}.{1}{2} build{3} {4}", v[0], v[1], v[2], v[3], "beta");
        }

        public static void GetIpAndMac(out string mac,out string ip)
        {

            mac = "";
            ip = "";
            ManagementClass mc;
            mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); 
            ManagementObjectCollection moc = mc.GetInstances(); 
            foreach (ManagementObject mo in moc) 
            {
                if ((bool)mo["IPEnabled"])
                {//取第一条记录
                    mac = mo["MACAddress"].ToString();
                    ip = ((string[])mo["IPAddress"])[0].ToString();
                    return;
                }
            }
           // return "";
        }
        public static void SendMyInfo(string mplayer)
        {
            try
            {
                string mac, ip;
                GetIpAndMac(out mac, out ip);
                SendMyInfo(mac, ip, mplayer);
            }
            catch { }
        }

        public static void SendMyInfo(string mac,string ip,string mplayer)
        {
            string os = System.Environment.OSVersion.ToString();
            string username = System.Environment.UserName;           
                ICuctv server = XmlRpcProxyGen.Create<ICuctv>();
                server.SetMyINfo(mac, ip, os, username, mplayer);
           
        }

      
    }
}
